<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="评论内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入评论内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="审核状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择审核状态:0待审核,1通过,2不通过" clearable size="small">-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="是否最佳" prop="isOptimum">
        <el-input
          v-model="queryParams.isOptimum"
          placeholder="请输入是否最佳"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入上级"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="祖级列表" prop="ancestors">
        <el-input
          v-model="queryParams.ancestors"
          placeholder="请输入祖级列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['forum:comment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['forum:comment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['forum:comment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['forum:comment:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="评论用户" align="center" prop="nickName" />
      <el-table-column label="动态" align="center" prop="dynamic" />
<!--      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="审核人员" align="center" prop="auditBy" />-->
<!--      <el-table-column label="是否最佳" align="center" prop="isOptimum" />-->
<!--      <el-table-column label="上级" align="center" prop="parentId" />-->
<!--      <el-table-column label="祖级列表" align="center" prop="ancestors" />-->
<!--      <el-table-column label="点赞数量" align="center" prop="zanNum" />-->
<!--      <el-table-column label="回复数量" align="center" prop="replyNum" />-->
<!--      <el-table-column label="媒体文件" align="center" prop="mediaFile" />-->
<!--      <el-table-column label="媒体类型" align="center" prop="mediaType" >-->
<!--        <template slot-scope="scope">-->
<!--          <span :class="'status_'+scope.row.mediaType">{{mediaFormat(scope.row)}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="审核状态" align="center" prop="status" >-->
<!--        <template slot-scope="scope">-->
<!--          <span :class="'status_'+scope.row.status">{{statusFormat(scope.row)}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="评论时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['forum:comment:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            v-if="scope.row.status != 2"-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleAnswer(scope.row)"-->
<!--            v-hasPermi="['zx:comment:edit']"-->
<!--          >回复</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['forum:comment:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改论坛-评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="!answer">
        <el-form-item label="评论内容" prop="content">
          <div>{{ form.content }}</div>
        </el-form-item>
        <el-form-item label="评论用户" prop="nickName">
          <div>{{ form.nickName }}</div>
        </el-form-item>
        <el-form-item label="动态" prop="dynamicId">
          <div>{{ form.dynamicId }}</div>
        </el-form-item>
<!--        <el-form-item label="审核状态">-->
<!--          <el-radio-group v-model="form.status">-->
<!--            <el-radio-->
<!--              v-for="dict in statusOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictValue"-->
<!--            >{{dict.dictLabel}}</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="评论时间" prop="commentTime">
          <div>{{ form.createTime }}</div>
        </el-form-item>
<!--        <el-form-item label="审核时间" prop="auditTime">-->
<!--          <el-date-picker clearable size="small" style="width: 200px"-->
<!--            v-model="form.auditTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择审核时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审核人员" prop="auditBy">-->
<!--          <el-input v-model="form.auditBy" placeholder="请输入审核人员" />-->
<!--        </el-form-item>-->
        <el-form-item label="是否最佳" prop="isOptimum">
          <div>{{ form.isOptimum!='0'?'是':'否' }}</div>
        </el-form-item>
        <el-form-item label="上级" prop="parentId">
          <div>{{ form.parentId }}</div>
        </el-form-item>
        <el-form-item label="点赞数量" prop="zanNum">
          <div>{{ form.zanNum }}</div>
        </el-form-item>
        <el-form-item label="回复数量" prop="replyNum">
          <div>{{ form.replyNum }}</div>
        </el-form-item>
        <el-form-item label="媒体文件" prop="mediaFile">
          <video v-if="form.mediaFile&&form.mediaType==1"
                 v-bind:src="base_url+form.mediaFile"
                 class="video-box"
                 controls="controls">
            您的浏览器不支持视频播放
          </video>
          <div v-else-if="form.mediaFile&&form.mediaType==0">
                 <el-image class="img" fit="cover"  v-bind:src="base_url+form.mediaFile" z-index="9999" :previewSrcList="[base_url+form.mediaFile]"></el-image>
          </div>
          <audio v-else-if="form.mediaFile&&form.mediaType==2"
          :src="base_url+form.mediaFile">
          </audio>
          <span v-else>暂无文件</span>
        </el-form-item>
        <el-form-item label="媒体类型">
          <el-select v-model="form.mediaType" placeholder="请选择媒体类型:0图片,1视频,2音频" :disabled="disable">
            <el-option v-for="dict in mediaTypeOptions"
                       :key="dict.dictValue"
                       :label="dict.dictLabel"
                       :value="dict.dictValue"/>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form  ref="answerForm" :model="form" :rules="rules" label-width="80px" v-else>
        <el-form-item label="回复" prop="rejectCause">
          <el-input  v-model="form.rejectCause" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComment, getComment, delComment, addComment, updateComment, exportComment } from "@/api/forum/comment";

export default {
  name: "Comment",
  data() {
    return {
      answer:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 论坛-评论表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      //是否禁止输入
      disable:false,
      // 是否显示弹出层
      open: false,
      //状态字典
      statusOptions:[],
      //数据类型字典
      mediaTypeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: undefined,
        wxUserId: undefined,
        dynamicId: undefined,
        status: undefined,
        commentTime: undefined,
        auditTime: undefined,
        auditBy: undefined,
        rejectCause: undefined,
        wxClientId: undefined,
        dataType: undefined,
        isOptimum: undefined,
        parentId: undefined,
        ancestors: undefined,
        zanNum: undefined,
        replyNum: undefined,
        mediaFile: undefined,
        mediaType: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      base_url: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.queryParams.dynamicId = this.$route.query.id;
    this.getList();
  },
  methods: {
    /** 查询论坛-评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getDicts("comment_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("zx_comment_media_type").then(response => {
        this.mediaTypeOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 审核状态(0待审核1通过2不通过)字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 审核状态(0待审核1通过2不通过)字典翻译
    mediaFormat(row, column) {
      return this.selectDictLabel(this.mediaTypeOptions, row.mediaType);
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        content: undefined,
        wxUserId: undefined,
        dynamicId: undefined,
        createTime: undefined,
        delFlag: undefined,
        status: "0",
        commentTime: undefined,
        auditTime: undefined,
        auditBy: undefined,
        rejectCause: undefined,
        wxClientId: undefined,
        dataType: undefined,
        isOptimum: undefined,
        parentId: undefined,
        ancestors: undefined,
        zanNum: undefined,
        replyNum: undefined,
        mediaFile: undefined,
        mediaType: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    handleAnswer(row){
      this.answer = true;
      this.open = true;
      this.title = "回复";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加论坛-评论";
      this.disable = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disable = true;
      const id = row.id || this.ids
      getComment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改论坛-评论";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateComment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addComment(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除论坛-评论编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delComment(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有论坛-评论数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportComment(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style scoped>
.status_0{
  color: gray!important;
}
.status_1{
  color: green;
}
.status_2{
  color: red;
}
.img{
  width: 100px;
  height: 100px;
}
.video-box{
  width: 150px;
  height: 200px;
}
</style>
